#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Magpie - an open source discussion forum system.
#
# Copyright (C) 2009 The Magpie Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ajiaojr.commons.django.shortcuts import render_to_response

from ajiaojr.magpie import forms, models
from ajiaojr.magpie.views import build_template_params
from ajiaojr.magpie.views import club_enabled_required
from ajiaojr.magpie.views import club_writable_required
from ajiaojr.magpie.views import forum_readable_required
from ajiaojr.magpie.views import forum_moderator_required
from ajiaojr.magpie.views import forum_required

from django.http import HttpResponseRedirect

from google.appengine.api import users

@club_enabled_required
@forum_required
@forum_readable_required
def index(request, forum_id, first_post_id = None):
  if first_post_id == '':
    first_post_id = None
    
  pinned = request.forum.get_pinned_threads()
  
  posts, next_id = request.forum.get_non_pinned_threads(starting_index = first_post_id,
                                                        count=20)

  p = build_template_params(request.forum.name, '/', request.user)
  p['forum'] = request.forum
  if next_id is not None:
    p['next_index'] = next_id
  p['posts'] = posts
  p['pinned'] = pinned
  
  if (request.user is not None):
    if (users.is_current_user_admin()
        or request.forum.is_moderator(request.user)):
      p['admin_mode'] = True

  return render_to_response(request, 'forum/index.html', p)

@club_writable_required
@forum_required
@forum_moderator_required
def manage(request, forum_id):
  p = build_template_params('Moderate', '/', request.user)
  p['forum'] = request.forum
  return render_to_response(request, 'forum/manage/index.html', p)

@club_writable_required
@forum_required
@forum_moderator_required
def manage_members(request, forum_id):
  p = build_template_params('Manage Members', '/', request.user)
  p['forum'] = request.forum
  return render_to_response(request, 'forum/manage/members/index.html', p)

@club_writable_required
@forum_required
@forum_moderator_required
def manage_members_add(request, forum_id):
  if request.method == 'POST':
    f = forms.UserInGroupForm(request.POST,
                              instance = models.UserInGroup(club_user = request.user,
                                                            user_group = request.forum.user_group))
    if f.is_valid():
      user_in_group = f.save(commit=False)
      request.forum.user_group.add_member(user_in_group.club_user)
      user_in_group.club_user.add_favourite(request.forum)
      return HttpResponseRedirect('/f/manage/%d/members' % request.forum.key().id())
    
  f = forms.UserInGroupForm()
  p = build_template_params('Add Member', '/', request.user)
  p['forum'] = request.forum
  p['form'] = f
  return render_to_response(request, 'forum/manage/members/add.html', p)
